import { ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";

export default function useDelete() {
  const idList = ref([]); //批量
  async function deleteHandler({ api, hideMessageBox = true, sendData, multiple = false }) {
    return new Promise(async (resolve, reject) => {
      try {
        if (multiple && !idList.value?.length) {
          ElMessage({
            type: "warning",
            message: "请至少选择一项！"
          });
          reject(false);
          return;
        }
        if (!hideMessageBox && ElMessageBox?.confirm) {
          await ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "danger"
          });
        }
        await api(sendData);
        ElMessage({
          type: "success",
          message: "删除成功"
        });
        idList.value = [];
        resolve(true);
      } catch (error) {
        reject(error);
      }
    });
  }

  //多选
  function select(data: any) {
    idList.value = data.idList;
  }

  return {
    deleteHandler,
    select,
    idList
  };
}
